﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace Pay.Token.Controllers.Api
{
    /// <summary>
    /// 集市发布商品
    /// </summary>
    [RoutePrefix("bazzar_product")]
    public class BazzarProductController : BaseController {
        private ITokenBazaarProductService _iTokenBazaarProductService;
        private ITokenBazaarOrderService _iTokenBazaarOrderService;
        private ICategoryService _iCategoryService;
        public BazzarProductController(ITokenBazaarProductService iTokenBazaarProductService, ICategoryService iCategoryService, ITokenBazaarOrderService iTokenBazaarOrderService) {
            _iTokenBazaarProductService = iTokenBazaarProductService;
            _iCategoryService = iCategoryService;
            _iTokenBazaarOrderService = iTokenBazaarOrderService;
        }
        /// <summary>
        /// 新增修改商品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("add_or_update_product"), HttpPost]
        public ResultMessage<bool> AddOrUpdateProduct(token_bazaar_product_base_dto model) {
            if (!ModelState.IsValid)
                return ResultToJson.Json<bool>(ModelError);
            model.merchant_id = UserId;
            bool result = _iTokenBazaarProductService.SaveProduct(model);
            return ResultToJson.Json(result ? "保存成功" : "保存失败", result);
        }
        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("get_product"), HttpGet]
        public ResultMessage<token_bazaar_product_dto> GetProduct(long id) {
            var result = _iTokenBazaarProductService.GetProduct(id);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns></returns>
        [Route("get_category"), HttpGet]
        public ResultMessage<List<token_category_dto>> GetCategory() {
            var result = _iCategoryService.GetTokenFirstDepthCatagory(1);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 集市定时收货
        /// </summary>
        /// <returns></returns>
        [Route("task_bazaar_order_recive"), HttpGet, AllowAnonymous, Anonymous]
        public async Task< ResultMessage<int>> TaskBazaarOrderRecive (){
            var result = await  _iTokenBazaarOrderService.SysTaskRecevie();
            return ResultToJson.Json(result);
        }
    }
}
